package com.lh.api.controller.shop;

import com.lh.api.annotation.Login;
import com.lh.api.controller.AbstractController;
import com.lh.api.form.ShopCartForm;
import com.lh.api.service.ShopCartService;
import com.lh.api.service.ShopService;
import com.lh.api.vo.ShopCartVo;
import com.lh.common.entity.admin.Shop;
import com.lh.common.enums.IsDel;
import com.lh.common.enums.UpperLowerShelfStatus;
import com.lh.common.utils.AssertUtil;
import com.lh.common.utils.Constant;
import com.lh.common.utils.PageUtils;
import com.lh.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Gze
 * @ClassName: ShopCartAction
 * @Description: (商品管理)
 * @date: 2020-05-18 14:07
 */

@Api(value="ShopCartAction",tags={"购物车相关接口"})
@RestController
@RequestMapping("/api/shopCart")
public class ShopCartAction extends AbstractController {

    @Autowired
    private ShopService shopService;

    @Autowired
    private ShopCartService shopCartService;

    @ApiOperation(value = "加入购物车,返回数据中data：购物车数量")
    @PostMapping("/addShopCart")
    @Login
    public Result<String> addShopCart(@RequestBody @Validated ShopCartForm shopCartForm){
        Long userId = getUserId();
        Shop shop = shopService.getById(shopCartForm.getSid());
        AssertUtil.assertNotNull(shop, Constant.RESULT.FI1000, "Item does not exist!");
        AssertUtil.assertNotTrue(UpperLowerShelfStatus.LOWERSHELF.equals(shop.getStatus()), Constant.RESULT.FI1000, "Item has been removed!");
        AssertUtil.assertNotTrue(IsDel.YES.equals(shop.getIsDel()), Constant.RESULT.FI1000, "Item deleted.");

        Integer shopCartNum = shopCartService.addShopCart(userId, shopCartForm.getSid(), shopCartForm.getNum());
        return Result.ok(shopCartNum);
    }

    @ApiOperation(value = "购物车列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class)
    })
    @GetMapping(value = "/cartlist")
    @ResponseBody
    @Login
    public Result<ShopCartVo> cartlist(Integer pageNum, Integer pageSize){
        Map<String, Object> param = new HashMap<>();
        param.put("uid", getUserId());
        PageUtils<ShopCartVo> shopCartVoPageUtils = shopCartService.getCartlist(param, pageNum, pageSize);
        return Result.ok(shopCartVoPageUtils);
    }

    @ApiOperation(value = "购物车数量,返回数据中data：购物车数量")
    @GetMapping(value = "/cartNum")
    @ResponseBody
    @Login
    public Result<String> cartNum(){
        Integer shopCartNum =  shopCartService.cartNum(getUserId());
        return Result.ok(shopCartNum);
    }

    @ApiOperation(value = "删除购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "sid", value = "商品id多个用,隔开", required = true,dataTypeClass= String.class)
    })
    @GetMapping(value = "/delShopCart")
    @ResponseBody
    @Login
    public Result<String> delShopCart(String sid){
        shopCartService.delShopCart(getUserId(), sid);
        return Result.ok();
    }

}
